# Submission for David Lin
# Created on 2013-08-17

class Player:
    def __init__(self):
        self.round_count = 1  # internal counter of rounds finished (since I'm unsure if round_number will start at 0 or 1)

    def hunt_choices(self,
                     round_number,
                     current_food,
                     current_reputation,
                     m,
                     player_reputations):
        self.num_players_left = len(player_reputations) + 1  # include yourself!

        # always slack in 2 player endgame (dominant strategy)
        if self.num_players_left is 2:
            return ['s']*len(player_reputations)

        # hunt with everyone for first 2 rounds (until we can figure out what the pool's behavior is)
        if round_number <= 2:
            return ['h']*len(player_reputations)

        ####################

        # Simple model of how people behave:
        # "freeloaders" never hunt (near 0.0 reputation)
        # "pushovers" always hunt (near 1.0 reputation)
        # "fairhunters" hunt for someone given their reputation (i.e. if someone has a reputation of 0.7, then a fairhunter will
        # hunt for them ~70% of the time)

        # Given the list of player_reputations, classify each person has a freeloader, pushover, or fairhunter (people can be a mixture of roles).
        # The ideal fairhunter has a reputation R = mean(player_reputations). Ignore our own reputation for simplicity / approximation of R.
        # Classify a person as a mixture of the three roles given where their reptuation lies (i.e. between 0.0 and R or between R and 1.0)

        # Example: Suppose R = 0.6. Then a player with a reputation of 0.7 will be considered 75% fairhunter and 25% pushover

        # Given the total number of freeloaders, pushovers, and fairhunters, we can solve whether it's more beneficial to be a freeloader
        # or pushover (i.e. whether it would be better for us to hunt for everyone and have a reputation of 1, or to hunt for no one)

        R = sum(player_reputations)/float(len(player_reputations))  # average reputation of everyone

        # If R is small, then assume we're at a nearly all freeloader endgame, so always slack
        if R < 0.01:
            return ['s']*len(player_reputations)

        # If R is very large, then assume we're near an all pushover endgame so always hunt
        # stricter condition for R near 1.0 since always hunting might not be a great strategy in this scenario
        # Likely will never get here since in this environment, almost everyone should stay alive
        if R > 0.999:
            return ['h']*len(player_reputations)

        num_freeloaders = sum((R - rep)/R if rep <= R else 0 for rep in player_reputations)
        num_pushovers = sum((rep - R)/(1.0 - R) if rep >= R else 0 for rep in player_reputations)
        num_fairhunters = len(player_reputations) - num_freeloaders - num_pushovers

        # If we act as a pushover (rep near 100%), then we'll get 3*#fairhunters food, but we'll have to pay 1 to each other player. Also, we lose
        # an additional 3 since all other players will gain 3 on us (compared to if we free-loaded)
        # Thus, we can calculate whether it's better to have a reputation near 0% or 100%:
        if num_fairhunters*3 - 3 - (self.num_players_left - 1) > 0:
            return ['h']*len(player_reputations)
        else:
            return ['s']*len(player_reputations)


    def hunt_outcomes(self, food_earnings):
        pass


    def round_end(self, award, m, number_hunters):
        self.round_count += 1
